<?php
    session_start();
    require_once('userlevel.php'); //Require/include the permissions file
    require_once('template.php'); //Require/include the template file
    require_once('functions.php'); //Require/include the functions file
    require_once('db.php'); //Require/include the file with the database connection information
     
    if (!$_SESSION['user_loggedin']) {
        die ("Not logged in... <script>document.location.href='login.php'</script>");
    } elseif (!($_SESSION['userlevel'] >= $userlevel['displayfile'])) {
        die (errorpage ("Your user level is not high enough to use this feature!", 'Compare two files'));
    } else {
        $fileid = strip_tags(mysql_escape_string($_GET['id']));
        $id1 = strip_tags(mysql_escape_string($_POST['id1']));
        $id2 = strip_tags(mysql_escape_string($_POST['id2']));
        template_headtag("Viewing $filename");
        template_header();
        template_left();
         
        include_once 'classes/diff/Diff.php';
        include_once 'classes/diff/Diff/Renderer.php';
        include_once 'classes/diff/Diff/inline.php';

        function diff_compute($text1, $text2) {
            // transform text into array
            $lines1 = explode("\n", $text1);
            $lines2 = explode("\n", $text2);
            // create the diff
            $diff = &new Text_Diff($lines1, $lines2);
            // create the inline renderer object
            $renderer = &new Text_Diff_Renderer_inline();
            // render and return the diff
            return $renderer->render($diff);
        }
        function diff_parse($text, $extension) {             
            // see if any meaningful text is left
            if (!$text) {
                return 'The two versions are identical.';
            }
             
            if (strtolower($extension) == 'php') {
                $text = highlight_string($text, TRUE);
                 
            } else {
                $text = htmlspecialchars($text);
                $text = nl2br($text);
            }
            $text = str_replace(Array('__del__', '__delclose__', '__ins__', '__insclose__'), Array('<del>', '</del>', '<ins>', '</ins>', ), $text);
             
            return "<div class='basicbox'>".$text."</div>";
        }
        $db = new my_db;
         
        $db->query("SELECT * FROM trackedfiles WHERE id='$fileid'");
        $db->next_record();
        if ($db->nf() > 0) {
            $filename = strip_tags($db->f('filename'));
            $filepath = strip_tags($db->f('filepath'));
            $filetype = strip_tags($db->f('filetype'));
            $mime = explode("/", $filetype);
            $filearray = explode('.', $filename);
            if (count($filearray) > 1) {
                $ep = count($filearray) -1;
                $extension = $filearray[$ep];
            }
             
            echo "<div class='subheadertext'>Viewing comparison of Revision#<a href='view.php?id=$fileid&amp;rev=$id1'>$id1</a> and Revision#<a href='view.php?id=$fileid&amp;rev=$id2'>$id2</a> of</div>";
            echo "<div class='subheadertext'>$filename</div><br/>";
            if ($mime[0] == "text") {
                $db->query("SELECT * FROM content WHERE dataid ='$id1'");
                $db->next_record();
                $text1 = $db->f("content");
                
                $db->query("SELECT * FROM content WHERE dataid ='$id2'");
                $db->next_record();
                $text2 = $db->f("content");
                 
                if (!empty($text1) AND !empty($text2)) {
                    $diff = diff_compute($text1, $text2);
                    
                    echo "";
                    echo diff_parse($diff, $extension);
                    echo "<br/>";
                     
                } else {
                    errorbox('Content not found!');
                }
            }
             
             
        } else {
            errorbox('File not found!');
        }
        template_footer();
    }
?>
